﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace FeedzillaArticles
{
    class Program
    {

        static void Main(string[] args)
        {
            Console.WriteLine("Enter a query string:");
            string query = Console.ReadLine();

            Console.WriteLine("Enter number of results:");
            int resultCount = int.Parse(Console.ReadLine());

            Console.WriteLine("Your query is being processed...");
            CreateQuery(query, resultCount);

            Console.WriteLine("Press Enter to Exit");
            Console.ReadLine();
        }

        private static async void CreateQuery(string query, int resultCount)
        {
            if (resultCount < 1 || resultCount > 100)
            {
                resultCount = 10;
            }

            //http://api.feedzilla.com/v1/articles/search.json?q=Michael
            var httpClient = new HttpClient();

            httpClient.BaseAddress = new Uri("http://api.feedzilla.com/v1/");

            httpClient.DefaultRequestHeaders.Accept.Add(new
                MediaTypeWithQualityHeaderValue("application/json"));

            var response = await httpClient.GetAsync("articles/search.json?q=" + query + "&" + "count=" + resultCount);

            var articleQuery = await response.Content.ReadAsAsync<ArticlesQuery>();

            foreach (var article in articleQuery.Articles)
            {
                Console.WriteLine("Title: {0}\nAuthor: {3}\nSource: {1}\nURL: {2}\nPublish Date:{4}\n\n",
                    article.Title, article.Source, article.Url, article.Author == null ? "Unkown" : article.Author,
                    article.Publish_Date == null ? "Unkown" : article.Publish_Date.ToString());
            }

            //Console.WriteLine("Raw Results: \n" + response.Content.ReadAsStringAsync().Result);
        }
    }

    //"articles":[
 
    //    {               

    //            "author":"Michael Maloney",
    //            "publish_date":"Tue, 05 Oct 2010 11:30:00 +0100",
    //            "source":"television",
    //            "source_url":"http:\/\/www.tvsquad.com\/rss.xml",
    //            "summary":"<p>Filed under: <ahref=\"http:\/\/www.tvsquad.com\/category\/features\/\" ..... ",
    //            "title":"RACHEL MCADAMS DATING MICHAEL SHEEN? (Showbiz Spy)",
    //            "url":"http:\/\/news.feedzilla.com\/en_us\/stories\/15583098?q=michael&client_source=api&format=json"

        
    //    },]

    public class ArticlesQuery
    {
        public IEnumerable<Article> Articles { get; set; }
        public string Description { get; set; }
        public string Syndication_Url { get; set; }
        public string Title { get; set; }
    }

    public class Article
    {
        public string Author { get; set; }
        public DateTime Publish_Date { get; set; }
        public string Source { get; set; }
        public string Source_URL { get; set; }
        public string Summary { get; set; }
        public string Title { get; set; }
        public string Url { get; set; }
    }
}
